import tabbar from '../../utils/tabbar'
const app = getApp()
const api = wx.getStorageSync('api')
let user
Page({
  data: {
    active: 2,
    isChoose: '0',
    cart: [], // 购物车列表
    isEdit: false, // 编辑状态
    isLogin: true
  },

  onLoad() {
    this.init()
  },

  onShow() {
    this.init()
  },

  init() {
    user = wx.getStorageSync('user')
    // console.log(typeof wx.getStorageSync('user'), user==false)
    if (!user) {
      // console.log('未登录')
      this.setData({
        isLogin: false
      })
      return
    }
    this.getCartList()
  },

  /**
   * 获取购物车列表
   */
  getCartList() {
    const url = api.cart + `?userId=${user.id}&choose=false`
    app.$http.get(url).then(res => {
      if (res.data == null) {
        this.setData({
          cart: []
        })
      }
      this.setData({
        ...res.data
      })
    })
  },

  /**
   * 跳转登陆页面
   */
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  /**
   * 点击底部tabar
   */
  onTabbarChange(e) {
    tabbar.redirectTo(e.detail)
  },

  /**
   * 选中改变 
   */
  chooseChange(e) {
    let id = e.currentTarget.dataset.item.id
    let isChoose = e.currentTarget.dataset.item.isChoose
    isChoose == '1' ? isChoose = 'false' : isChoose = 'true'
    const url = api.car_chooseToggle + `?cartId=${id}&choose=${isChoose}`
    app.$http.post(url).then(res => {
      this.getCartList()
    })
  },

  /**
   * 全选商品
   */
  chooseAll() {
    let isChoose = this.data.isChoose ? 'false' : 'true'
    const url = api.car_chooseAll + `?userId=${user.id}&chooseAll=${isChoose}`
    app.$http.post(url).then(res => {
      this.getCartList()
    })
  },

  /**
   * 商品数量增加
   */
  numplus(e) {
    let id = e.currentTarget.dataset.id
    const url = api.car_num + `?cartId=${id}&isAdd=true`
    console.log(id, '1111', url)
    app.$http.post(url).then(res => {
      this.getCartList()
    })
  },

  /**
   * 商品数量减少
   */
  numminus(e) {
    let id = e.currentTarget.dataset.id
    const url = api.car_num + `?cartId=${id}&isAdd=false`
    console.log(id)
    app.$http.post(url).then(res => {
      this.getCartList()
    })
  },


  /**
   * 编辑商品
   */
  edit() {
    let cart = this.data.cart
    cart.forEach((item, index, array) => {
      item.isChoose = '0'
    })

    this.setData({
      isEdit: !this.data.isEdit,
      cart: cart,
      isChoose: '0'
    })

    if (!this.data.isEdit) {
      this.getCartList()
    }
  },

  /**
   * 编辑时选择商品
   */
  chooseChange_edit(e) {
    let id = e.currentTarget.dataset.item.id
    let isChoose = e.currentTarget.dataset.item.isChoose
    isChoose == '1' ? isChoose = '0' : isChoose = '1'
    let cart = this.data.cart
    let chooseAll = 1
    cart.forEach((item, index, array) => {
      if (item.id == id) {
        item.isChoose = isChoose
      }
      if (item.isChoose == 0) {
        chooseAll = 0
      }
    })
    this.setData({
      cart: cart,
      isChoose: chooseAll
    })
  },

  /**
   * 编辑时全选商品
   */
  chooseAll_edit(e) {
    let cart = this.data.cart
    let isChoose = this.data.isChoose
    isChoose == '1' ? isChoose = '0' : isChoose = '1'
    cart.forEach((item, index, array) => {
      item.isChoose = isChoose
    })
    this.setData({
      cart: cart,
      isChoose: isChoose
    })
  },

  /**
   * 提交删除
   */
  deleteSubmit() {
    let cart = this.data.cart
    let ids = []
    cart.forEach((item, index, array) => {
      if (item.isChoose == '1') {
        ids.push(item.id)
      }
    })
    console.log(ids)
    if (ids.length == 0) {
      wx.showToast({
        title: '未勾选商品',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: `确定要删除${ids.length}件商品吗`,
      success: (res) => {
        if (res.confirm) {
          let url = api.car_del
          let data = {
            ids: ids
          }
          app.$http.post(url, data).then(res => {
            this.getCartList()
            console.log(res)
            this.setData({
              isEdit: false
            })
          })
        }
      }
    })
  },

  notdo() {
    wx.showToast({
      title: '正在开发',
      icon: 'none',
      duration: 1000
    })
  },

  /**
   * 前往提交订单页
   */
  toSubmitOrder() {
    if (this.data.totalPrice == 0.00) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none',
        duration: 1000
      })
      return
    }
    wx.navigateTo({
      url: '/pages/submitOrder/submitOrder'
    })
  }
})